   
--*********************************************************************
--*  Exports material information
--*  
fn exportAmbientOcclusion pMesh = 
(
	
	
	select pMesh
	--We need a MR skylight to achieve good results
	local aoSkyLight = checkInsertAOLight ()
				
	--stops the render dialog from popping up after next line and sets the rendering engine to MR
	local quiteState = SetQuietMode (not true)
	renderers.production = RendererClass.classes[3] ()

	local dim =2048
	global StandardMaterial amboccShader = StandardMaterial() 
	global ambientShaderConnect = Ambient_Reflective_Occlusion__3dsmax()
	amboccShader.diffuseMap = ambientShaderConnect
	renderers.production.Enable_Material_Override = true
	renderers.production.Override_Material = amboccShader
	ambientShaderConnect.dark = backgroundColor
	ambientShaderConnect.bright = color 125 125 125
	ambientShaderConnect.samples = 32
	ambientShaderConnect.max_distance = 50
	ambientShaderConnect.spread = 0.5
	renderers.current.FinalGatherEnable2 = true
/*
	global mrmaterial amboccShader = mental_ray()
	global ambientShaderConnect = Ambient_Reflective_Occlusion__3dsmax()
	amboccShader.surface = ambientShaderConnect
	renderers.production.Enable_Material_Override = true
	renderers.production.Override_Material = amboccShader
	ambientShaderConnect.dark = backgroundColor
	ambientShaderConnect.bright = color 111 0 111
	ambientShaderConnect.samples = 32
	ambientShaderConnect.max_distance = 50
	ambientShaderConnect.spread = 0.5
	renderers.current.FinalGatherEnable2 = true
*/	
	 
	local pMeshBI = pMesh.INodeBakeProperties
	local pMeshBPI = pMesh.INodeBakeProjProperties
	pMeshBPI.enabled = false
	pMeshBPI.projectionMod             = undefined -- Get/Set the Projection Modifier
    pMeshBPI.projectionModTarget     =""-- Get/Set the Projection Modifier Target
    pMeshBPI.cropAlpha                     = false -- Get/Set whether Alpha will be cropped
    pMeshBPI.projSpace                     = #Raytrace --  Get/Set the Projection Space  projSpace enums: {#raytrace|#uvw_match} 
    pMeshBPI.normalSpace                 = #tangent -- Get/Set the Normal Space.  normalSpace enums: {#world|#screen|#local|#tangent} 
    pMeshBPI.tangentYDir                 = #Y_Down -- Get/Set the Y Tangent Direction. Get/Set the Y Tangent Direction.
    pMeshBPI.tangentXDir                 = #X_Right -- Get/Set the X Tangent Direction.  tangentXDir enums: {#X_Right|#X_Left}
    pMeshBPI.useCage                     = true -- Get/Set whether to use the Cage.
    pMeshBPI.rayOffset                     = 1.0 -- Get/Set the Ray Offset value.
    pMeshBPI.hitResolveMode             = #furthest -- Get/Set the Hit Resolve Mode. hitResolveMode enums: {#closest|#furthest}
    pMeshBPI.hitMatchMtlID             = false -- Get/Set whether to match the Material IDs when projecting.
    pMeshBPI.hitWorkingModel             = false -- 
    pMeshBPI.warnRayMiss                 = true -- Get/Set whether to warn of missed ray hits.
    pMeshBPI.rayMissColor                 = (color 255 0 0) -- Get/Set the warn color for missed ray hits.
    pMeshBPI.heightMapMin                 = -1.0 -- Get/Set the HeightMap Min. Value.
    pMeshBPI.heightMapMax             = 1.0 -- Get/Set the HeightMap Max. Value.
    pMeshBPI.heightBufMin                  = 0.0
    pMeshBPI.heightBufMax                 = 0.0
    pMeshBPI.BakeObjectLevel             = true
    pMeshBPI.BakeSubObjLevels         = true
    pMeshBPI.useObjectBakeForMtl     = true
    pMeshBPI.proportionalOutput         = false
    pMeshBPI.subObjBakeChannel         = 2

	--create uvw unwrap modifier for projection
	local unwrapMod = Unwrap_UVW()
 	
	local flattenAngle = 25.0
	local flattenSpacing = 0.001
	local flattenRotate = false
	local flattenFillHoles = true
	
 	unwrapMod.setAlwaysEdit true
 	unwrapMod.setMapChannel 2
 	unwrapMod.setFlattenAngle flattenAngle 
 	unwrapMod.setFlattenSpacing flattenSpacing 
 	unwrapMod.setFlattenNormalize true
 	unwrapMod.setFlattenRotate flattenRotate 
 	unwrapMod.setFlattenFillHoles flattenFillHoles 
	unwrapMod.setApplyToWholeObject true
 	unwrapMod.name = "EEExportUnproj"
 	unwrapMod.setDebugLevel 0

	select pMesh

	update pMesh
	select pMesh
	addModifier pMesh unwrapMod
	update pMesh
	select pMesh
	unwrapMod.flattenMapByMatID flattenAngle flattenSpacing true 0 flattenRotate flattenFillHoles 
	update pMesh
	select pMesh
	collapseStack pMesh 
	update pMesh
	select pMesh
	
--	pMeshBPI.projectionMod = unwrapMod

	--setup the RTT's AO features
	local aoFilename = ((pMesh.name as string) + "_ao_map")
	local aoElement = Ambient_Occlusion ()
	aoElement.autoSzOn = false
	aoElement.enabled = true
	aoElement.fileName = aoFilename
	aoElement.fileType = "jpg"
	aoElement.filterOn = true
	aoElement.outputSzX = dim
	aoElement.outputSzY = dim
	aoElement.samples =10
	aoElement.spread = 0.5
	
	local elementList = #(aoElement) 

	
	--setup the RTT features
	pMeshBI.bakeEnabled = true
	pMeshBI.bakeChannel = 2
	pMeshBI.addBakeElement aoElement

	--render the AO map the the project's content directory
	local aoFilepath = GetProjectContentDir() + "textures\\" + aoFilename + ".jpg"
	render rendertype:#bakeselected outputwidth:dim outputheight:dim outputfile:aoFilepath renderElements:true renderElementBitmaps:&elementList vfb:on quiet:true disableBitmapProxies:true
	
	pMeshBI.removeAllBakeElements()
--	pMeshBI.bakeEnabled = false

	if (aoSkyLight != OK) then
		delete aoSkyLight
	SetQuietMode quiteState
)	
--*********************************************************************
--*  Exports material information
--*  
fn exportMaterial pMaterial = 
(
	if pMaterial == undefined then
		return
	
	local opacity = undefined
	opacity = 100.0 -- (pMaterial.opacity as float)

	writeFileStr (filenameFromPath pMaterial.name)
	writefloat gFilestream opacity
	
	if pMaterial.diffuseMap  == undefined then
		writeFileStr (undefined)
	else
		writeFileStr (filenameFromPath pMaterial.diffuseMap.filename)
	
	if pMaterial.bumpMap  == undefined then
		writeFileStr (undefined)
	else
		writeFileStr (filenameFromPath pMaterial.bumpMap.filename)
	
	if pMaterial.specularLevelMap == undefined then
		writeFileStr (undefined)
	else
		writeFileStr (filenameFromPath pMaterial.specularLevelMap.filename)

	if pMaterial.opacityMap  == undefined then
		writeFileStr (undefined)
	else
		writeFileStr (filenameFromPath pMaterial.opacityMap.filename)

)
